<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Utils\Time;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;


class IndexController extends Controller
{
    /*****************************模板图片*********************************/
    public function filetemplate ()
    {
        return view('admin.index.blessing.filetemplate');
    }

    /**
     * 添加模板图片
     * @param Request $request
     * @return \App\Http\Utils\Response
     */
    public function template (Request $request)
    {
        $data = $request->all();
        $rules = [
            'type'           => 'required',
            'path'          => 'required',
        ];
        $messages = [
            'type.required'          => '请选择类型',
            'path.required'          => '请上传路径',
        ];
        $validator = Validator::make($data, $rules, $messages);
        if ($validator->fails()) {
            return $this->json('',417,$validator->getMessageBag()->getMessages());
        }
        if (is_array($data['path'])) {
            foreach($data['path'] as $k=>$v) {
                DB::table('template_picture')
                    ->insert([
                        'type' => $data['type'],
                        'path' => $v['path'],
                        'create_time'  => Time::current(),
                    ]);
            }

        } else {
            DB::table('template_picture')
                ->insert([
                    'type' => $data['type'],
                    'path' => $data['path'],
                    'create_time'  => Time::current(),
                ]);
        }
        return $this->json('',200,'ok');
    }

    public function delTemplate ($id) {

        if (DB::table('template_picture')->delete($id)) {
            return $this->json('',200,'删除成功');
        } else {
            return $this->json('',417,'删除失败');
        }
    }
}
